package com.xl.utils.configuration;

import com.xl.utils.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author xionglin
 * @Classname MyControllerAdvice
 * @Description 全局处理异常
 * @Date 2019/9/19 09:58
 */
@Slf4j
@ControllerAdvice
@Configuration
public class MyControllerAdvice {

    /**
     * 全局异常捕捉处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map errorHandler(Exception e) {
        Map<String, Object> map = ResultUtil.map("501", e.getMessage(), "发生异常");
        log.error(e.getMessage(),e);
        return map;
    }
}
